package com.ys.web.goods.order.service;

import com.ys.base.BaseParam;
import com.ys.utils.base.j2se.Convert;
import com.ys.utils.base.j2se.DateUtil;
import com.ys.utils.base.j2se.MapHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.datasource.DataSourceBy;
import com.ys.utils.spring.config.SystemError;
import com.ys.web.goods.order.mapper.OrderMapper;
import com.ys.web.goods.order.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderService{
    @Autowired
    private OrderMapper orderMapper;

    Map<Long,List<Long>> goods = new HashMap<>();

//    @PostConstruct
//    void init(){
//        initGoods();
//    }
//
//    @Override
//    public void run() {
//        try {
//            Thread.sleep(3600);
//            initGoods();
//        } catch (InterruptedException e) {
//            Logger.error("初始化化商品信息失败",e);
//        }
//    }

//    private void initGoods(){
//        DynamicDataSourceContextHolder.setDataSourceKey("ysGoods");
//        List<Map<String,Long>> goodsList = orderMapper.getAllGoods();
//        DynamicDataSourceContextHolder.clearDataSourceKey();
//        Map<Long,List<Map<String,Long>>> ml = goodsList.stream().collect(Collectors.groupingBy(x->StringHandler.yuanToFen(StringHandler.getString(x.get("price")))));
//        ml.forEach((k,v)-> goods.put(k,v.stream().map(x->x.get("id")).collect(Collectors.toList())));
//    }

    /**
     * 生成订单信息并进行支付
     * @param money 购买总金额
     */
    @DataSourceBy("ysGoods")
    public Map<String,String> order(String uid, String money,int payChannel){
        List<Long> goodsList = orderMapper.getAllGoods(Convert.objToInt(money)/100);
//        List<Long> goodsIdList = goods.get(StringHandler.getLong(money));
//        if(StringHandler.isEmpty(goodsIdList)){
//            goodsIdList = goods.get(StringHandler.getLong(1000));
//        }


        int i = (int) (Math.random()*(goodsList.size()));
        long goodsId = goodsList.get(i);
        Map<String,Object> goods = orderMapper.goods(goodsId);
        String unit = "";
        if(StringHandler.isNotEmpty(goods.get("units"))){
            List<String> units = Arrays.asList(StringHandler.getString(goods.get("units")).split(","));
            int ii = (int) (Math.random()*(units.size()));
            unit = units.get(ii);
        }

        String finalUnit = unit;
        Map<String,Object> order = new HashMap<String,Object>(){{
            put("order_sn", BaseParam.getOrderSN());
            put("order_amount", money);
            put("date", DateUtil.getDate());
            put("goods_id",goods.get("id"));
            put("goods_name",goods.get("name"));
            put("goods_pic",goods.get("cover_pic"));
            put("goods_category_pid",goods.get("category_pid"));
            put("goods_units", finalUnit);
            put("goods_price",goods.get("price"));
            put("goods_inner_price",goods.get("price"));
            put("goods_num",1);
            put("goods_is_discount",goods.get("is_discount"));
            put("uid",uid);
            put("role",0);
            put("address_id",0);
            put("type",2);
            put("btype",2);
            put("payChannel",payChannel);
        }};
        orderMapper.order(order);
        int goodsCount = orderMapper.goodsInventoryChange(StringHandler.getLong(goods.get("id")),1);
        if(goodsCount!=1) SystemError.wrapBs("6001","网络异常");
        return MapHandler.initMap("id",StringHandler.getString(order.get("id")),"goodsName",StringHandler.getString(order.get("goods_name")));
    }

    /**
     * 修改订单信息
     * @param orderId
     */
    @DataSourceBy("ysGoods")
    public int orderStatus(String orderId){
        return orderMapper.orderStatus(orderId);
    }

    @DataSourceBy("ysGoods")
    public int orderPayStatus(String orderSn,int status,long payTime,String linkId,String tradeSn){
        return orderMapper.orderPayStatus(orderSn,status,payTime,linkId,tradeSn);
    }
    @DataSourceBy("ysGoods")
    public int goodsOrderRefundUpdate(String orderId,String refundSn){
        return orderMapper.goodsOrderRefundUpdate(orderId,refundSn);
    }
    /**
     * 修改订单信息
     * @param orderId
     */
    @DataSourceBy("ysGoods")
    public int orderTimeOut(String orderId){
        return orderMapper.orderTimeOut(orderId);
    }
    @DataSourceBy("ysGoods")
    public Map<String,Object> goodsOrderInfo(long orderId,int status){
      return orderMapper.goodsOrderInfo(orderId,status);
    }

    @DataSourceBy("ysGoods")
    public Order goodsOrderSnInfo(String orderSn,int status){
        return orderMapper.goodsOrderSnInfo(orderSn,status);
    }

    @DataSourceBy("ysGoods")
    public int goodsOrderUpdate(long orderId, String collectionUid,int status) {
        return orderMapper.goodsOrderUpdate(orderId,collectionUid,status);
    }
    @DataSourceBy("ysGoods")
    public int refundStatusUpdate(String orderId) {
        return orderMapper.refundStatusUpdate(orderId);
    }

    @DataSourceBy("ysGoods")
    public int goodsOrderRefund(long orderId, String refundSn, long refundTime) {
        return orderMapper.goodsOrderRefund(orderId,refundSn,refundTime);
    }

    @DataSourceBy("ysGoods")
    public int goodsOrderFailed(long orderId, String refundSn, long refundTime) {
        return orderMapper.goodsOrderFailed(orderId,refundSn,refundTime);
    }

    @DataSourceBy("ysGoods")
    public Order getOrderLid(long uid, String lid) {
        return orderMapper.getOrderLid(uid,lid);
    }
}
